import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../http/api.dart';
import '../../language.dart';

class RegisterLogic extends GetxController {
  var userName = "".obs;
  var passWord = "".obs;
  var isHidePwd = true.obs;
  var inviteCode = "".obs;

  FocusNode focusNode1 = FocusNode();
  FocusNode focusNode2 = FocusNode();
  FocusNode focusNode3 = FocusNode();

  register() async {
    FocusManager.instance.primaryFocus?.unfocus();

    if (userName.value.isEmpty) {
      EasyLoading.showToast(LanguageMsg.loginInputEmailTips.tr);
      return;
    }
    if (passWord.value.length < 6) {
      EasyLoading.showToast(LanguageMsg.loginInputPwdTips.tr);
      return;
    }
    EasyLoading.show(status: 'loading...');
    dynamic result = await Api.register({
      'username': userName.value,
      'password': passWord.value,
      'invite_code': inviteCode.value,
    });
    EasyLoading.dismiss();
    if (result['code'] == 1) {
      EasyLoading.showSuccess('Register success');
      Get.back(result: true);
    }
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    FocusManager.instance.primaryFocus?.unfocus();
    super.onClose();
  }
}
